package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.Employees;
import com.heima.homeservicemanagementsystem.entity.ServiceClassification;
import com.heima.homeservicemanagementsystem.entity.SysUser;
import com.heima.homeservicemanagementsystem.service.EmployeesService;
import com.heima.homeservicemanagementsystem.service.ServiceClassificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.EmployeesAndService;
import com.heima.homeservicemanagementsystem.service.EmployeesAndServiceService;
import com.heima.homeservicemanagementsystem.mapper.EmployeesAndServiceMapper;

import java.util.List;

import static cn.dev33.satoken.SaManager.log;

/**
 * 从业人员所对的服务;(employees_and_service)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-23
 */
@Tag(name = "从业人员所对的服务对象功能接口")
@RestController
@RequestMapping("/employeesAndService")
public class EmployeesAndServiceController {

    @Resource
    private EmployeesAndServiceService employeesAndServiceService;
    @Resource
    private EmployeesService employeesService;
    @Resource
    private EmployeesAndServiceMapper employeesAndServiceMapper;
    @Resource
    private ServiceClassificationService serviceClassificationService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<EmployeesAndService>> paginQuery(@RequestBody PageParams<EmployeesAndService> pageParams) {
        //查询参数
        EmployeesAndService params = pageParams.getParams();
        LambdaQueryWrapper<EmployeesAndService> lqw = new LambdaQueryWrapper<>();


        lqw.eq(params.getUserId() != null, EmployeesAndService::getUserId, params.getUserId());


        lqw.eq(params.getServiceId() != null, EmployeesAndService::getServiceId, params.getServiceId());

        return Result.success(employeesAndServiceService.page(pageParams.getPage(), lqw));
    }


    @Operation(summary = "从业人员所对的服务所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        List<EmployeesAndService> list = employeesAndServiceService.list();
        for (EmployeesAndService employeesAndService : list) {
            Employees id = employeesService.getOne(new QueryWrapper<Employees>().eq("id", employeesAndService.getUserId()));
            ServiceClassification id1 = serviceClassificationService.getOne(new QueryWrapper<ServiceClassification>().eq("id", employeesAndService.getServiceId()));
            employeesAndService.setEmployees(id);
            employeesAndService.setServiceClassification(id1);
        }

        return Result.success(list);
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody EmployeesAndService employeesAndService) {
        LambdaQueryWrapper<EmployeesAndService> lqw = new LambdaQueryWrapper<>();


        lqw.eq(employeesAndService.getId() != null, EmployeesAndService::getId, employeesAndService.getId());


        lqw.eq(employeesAndService.getUserId() != null, EmployeesAndService::getUserId, employeesAndService.getUserId());


        lqw.eq(employeesAndService.getServiceId() != null, EmployeesAndService::getServiceId, employeesAndService.getServiceId());
        List<EmployeesAndService> list = employeesAndServiceService.list(lqw);

        for (EmployeesAndService emservice : list) {
            Employees id = employeesService.getOne(new QueryWrapper<Employees>().eq("id", emservice.getUserId()));
            ServiceClassification id1 = serviceClassificationService.getOne(new QueryWrapper<ServiceClassification>().eq("id", emservice.getServiceId()));
            emservice.setEmployees(id);
            emservice.setServiceClassification(id1);
        }
        return Result.success(list);
    }

    /*
     * 获取服务的内容
     *
     * @param null
     * @return
     * @author jinchengfei
     * @create 2025/4/23
     **/
    @Operation(summary = "获取从业人员的服务分类")
    @PostMapping("/getService")
    public Result getService(@RequestBody EmployeesAndService employeesAndService) {
        log.info("用户信息", employeesAndService);
        EmployeesAndService one = employeesAndServiceService.getOne(new LambdaQueryWrapper<EmployeesAndService>().eq(EmployeesAndService::getUserId, employeesAndService.getUserId()));
        String serviceId = one.getServiceId();
        ServiceClassification one1 = serviceClassificationService.getOne(new LambdaQueryWrapper<ServiceClassification>().eq(ServiceClassification::getId, serviceId));
        return Result.success(one1);
    }


    /**
     * 新增数据
     *
     * @param employeesAndService 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增相关的绑定")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody EmployeesAndService employeesAndService) {
        log.info("用户信息", employeesAndService);
        List<EmployeesAndService> list = employeesAndServiceService.list(Wrappers.<EmployeesAndService>lambdaQuery().eq(EmployeesAndService::getUserId, employeesAndService.getUserId()));
        if (list.size() > 0) {

//            return Result.error("该用户已经绑定服务");
            UpdateWrapper<EmployeesAndService> objectUpdateWrapper = new UpdateWrapper<>();
            objectUpdateWrapper.eq("user_id", employeesAndService.getUserId());
            objectUpdateWrapper.set("service_id", employeesAndService.getServiceId());
            employeesAndServiceService.update(null, objectUpdateWrapper);
            return Result.success();
        } else {
            if (employeesAndServiceService.save(employeesAndService)) {
                return Result.success();
            }
        }

        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param employeesAndService 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody EmployeesAndService employeesAndService) {
        if (employeesAndServiceService.updateById(employeesAndService)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (employeesAndServiceService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (employeesAndServiceService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}